home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Jan / di9801rs / Sortalgs.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-20  |  5.9 KB  |  212 lines

  1. unit SortAlgs;
  2.  
  3. interface
  4.  
  5. uses
  6.     Dialogs;
  7.  
  8. type
  9.     ValueType = Longint; // Type used in the arrays.
  10.     IndexType = Longint; // Type used to index arrays.
  11.     TValueArray =  array[0..100000000] of ValueType;
  12.     PValueArray = ^TValueArray;
  13.     TCountArray =  array[0..100000000] of IndexType;
  14.     PCountArray = ^TCountArray;
  15.  
  16.     procedure Bubblesort(var List : array of ValueType; min, max : IndexType);
  17.     procedure Selectionsort(var List : array of ValueType; min, max : IndexType);
  18.     procedure Quicksort(var List : array of ValueType; min, max : IndexType);
  19.     procedure Countingsort(var List, SortedList : array of ValueType; min, max : IndexType; min_value, max_value : ValueType);
  20.  
  21. implementation
  22.  
  23. // Run bubblesort.
  24. procedure Bubblesort(var List : array of ValueType; min, max : IndexType);
  25. var
  26.     last_swap, i, j : IndexType;
  27.     tmp             : ValueType;
  28. begin
  29.     // Repeat until we are done.
  30.     while (min < max) do
  31.     begin
  32.         // Bubble up.
  33.         last_swap := min - 1;
  34.         // for i := min + 1 to max
  35.         i := min + 1;
  36.         while (i <= max) do
  37.         begin
  38.             // Find a bubble.
  39.             if (List[i - 1] > List[i]) then
  40.             begin
  41.                 // See where to drop the bubble.
  42.                 tmp := List[i - 1];
  43.                 j := i;
  44.                 repeat
  45.                     List[j - 1] := List[j];
  46.                     j := j + 1;
  47.                     if (j > max) then Break;
  48.                 until (List[j] >= tmp);
  49.                 List[j - 1] := tmp;
  50.                 last_swap := j - 1;
  51.                 i := j + 1;
  52.             end else
  53.                 i := i + 1;
  54.         end; // while (i <= max) do.
  55.         // End bubbling up.
  56.  
  57.         // Update max.
  58.         max := last_swap - 1;
  59.  
  60.         // Bubble down.
  61.         last_swap := max + 1;
  62.         // for i := max - 1 downto min
  63.         i := max - 1;
  64.         while (i >= min) do
  65.         begin
  66.             // Find a bubble.
  67.             if (List[i + 1] < List[i]) then
  68.             begin
  69.                 // See where to drop the bubble.
  70.                 tmp := List[i + 1];
  71.                 j := i;
  72.                 repeat
  73.                     List[j + 1] := List[j];
  74.                     j := j - 1;
  75.                     If j < min Then Break;
  76.                 until (List[j] <= tmp);
  77.                 List[j + 1] := tmp;
  78.                 last_swap := j + 1;
  79.                 i := j - 1;
  80.             end else
  81.                 i := i - 1;
  82.         end; // while (i >= min) do
  83.         // End bubbling down.
  84.  
  85.         // Update min.
  86.         min := last_swap + 1;
  87.     end; // while (min < max) do
  88. end;
  89.  
  90. // Run selectionsort.
  91. procedure Selectionsort(var List : array of ValueType; min, max : IndexType);
  92. var
  93.     i, j, best_j : IndexType;
  94.     best_value   : ValueType;
  95. begin
  96.     for i := min to max - 1 do
  97.     begin
  98.         best_value := List[i];
  99.         best_j := i;
  100.         for j := i + 1 to max do
  101.         begin
  102.             if (List[j] < best_value) Then
  103.             begin
  104.                 best_value := List[j];
  105.                 best_j := j;
  106.             end;
  107.         end;    // for j := i + 1 to max do
  108.         List[best_j] := List[i];
  109.         List[i] := best_value;
  110.     end;        // for i := min to max - 1 do
  111. end;
  112.  
  113. // Run quicksort.
  114. procedure Quicksort(var List : array of ValueType; min, max : IndexType);
  115. var
  116.     med_value : ValueType;
  117.     hi, lo, i : IndexType;
  118. begin
  119.     // If the list has <= 1 element, it's sorted.
  120.     if (min >= max) then Exit;
  121.  
  122.     // Pick a dividing item randomly.
  123.     i := min + Trunc(Random(max - min + 1));
  124.     med_value := List[i];
  125.  
  126.     // Swap it to the front so we can find it easily.
  127.     List[i] := List[min];
  128.  
  129.     // Move the items smaller than this into the left
  130.     // half of the list. Move the others into the right.
  131.     lo := min;
  132.     hi := max;
  133.     while (True) do
  134.     begin
  135.         // Look down from hi for a value < med_value.
  136.         while (List[hi] >= med_value) do
  137.         begin
  138.             hi := hi - 1;
  139.             if (hi <= lo) then Break;
  140.         end;
  141.         if (hi <= lo) then
  142.         begin
  143.             // We're done separating the items.
  144.             List[lo] := med_value;
  145.             Break;
  146.         end;
  147.  
  148.         // Swap the lo and hi values.
  149.         List[lo] := List[hi];
  150.  
  151.         // Look up from lo for a value >= med_value.
  152.         lo := lo + 1;
  153.         while (List[lo] < med_value) do
  154.         begin
  155.             lo := lo + 1;
  156.             if (lo >= hi) then Break;
  157.         end;
  158.         if (lo >= hi) then
  159.         begin
  160.             // We're done separating the items.
  161.             lo := hi;
  162.             List[hi] := med_value;
  163.             Break;
  164.         end;
  165.  
  166.         // Swap the lo and hi values.
  167.         List[hi] := List[lo];
  168.     end; // while (True) do
  169.  
  170.     // Sort the two sublists.
  171.     Quicksort(List, min, lo - 1);
  172.     Quicksort(List, lo + 1, max);
  173. end;
  174.  
  175. // Run countingsort.
  176. procedure Countingsort(var List, SortedList : array of ValueType; min, max : IndexType; min_value, max_value : ValueType);
  177. var
  178.     i, j, next_index : IndexType;
  179.     count_index      : ValueType;
  180.     counts           : PCountArray;
  181. begin
  182.     // Create the Counts array.
  183.     GetMem(counts, (max_value - min_value + 1) * SizeOf(IndexType));
  184.  
  185.     // Initialize the counts to zero.
  186.     for i := 0 to max_value - min_value do
  187.         counts[i] := 0;
  188.  
  189.     // Count the items.
  190.     for i := min to max do
  191.     begin
  192.         count_index := List[i] - min_value;
  193.         counts[count_index] := counts[count_index] + 1;
  194.     end;
  195.  
  196.     // Place the items in the sorted array.
  197.     next_index := min;
  198.     for i := min_value to max_value do
  199.     begin
  200.          for j := 1 to counts[i - min_value] do
  201.          begin
  202.              SortedList[next_index] := i;
  203.              next_index := next_index + 1;
  204.          end;
  205.     end;
  206.  
  207.     // Free the memory allocated for the counts array.
  208.     FreeMem(counts);
  209. end;
  210.  
  211. end.
  212.